package com.example.tool.subject;

import com.example.tool.utils.SwingUtil;
import com.intellij.ide.util.projectWizard.ModuleWizardStep;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.ui.TextBrowseFolderListener;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.ui.components.JBTextField;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.intellij.util.ui.JBUI;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * <p>Title: SubjectModuleWizardStep</p >
 * Description: 自定义模块的构建页面配置
 * <p>CreateTime: 2022/6/21 9:28</p>
 * @author FT
 */
@SuppressWarnings("unused")
public class HToolModuleWizardStep extends ModuleWizardStep {
  private static HToolModuleWizardStep hToolModuleWizardStep;
  private TextFieldWithBrowseButton textFieldWithBrowseButton;
  private final JBTextField pluginID = new JBTextField();
  private final JBTextField pluginName = new JBTextField();
  private final JBTextField pluginCategory = new JBTextField();
  private final JBTextField staticDirectory = new JBTextField();
  private final JBTextField remotePort = new JBTextField();
  private final String directory = "WebContent";
  private final String port = "5005";

  private HToolModuleWizardStep() {
  }

  public static HToolModuleWizardStep getInstance() {
    if (hToolModuleWizardStep == null) {
      hToolModuleWizardStep = new HToolModuleWizardStep();
    }
    return hToolModuleWizardStep;
  }


  public TextFieldWithBrowseButton getTextFieldWithBrowseButton() {
    return textFieldWithBrowseButton;
  }

  public JBTextField getPluginID() {
    return pluginID;
  }

  public JBTextField getPluginName() {
    return pluginName;
  }

  public JBTextField getPluginCategory() {
    return pluginCategory;
  }

  public JBTextField getStaticDirectory() {
    return staticDirectory;
  }

  public JBTextField getRemotePort() {
    return remotePort;
  }

  @Override
  @SuppressWarnings("All")
  public JComponent getComponent() {
    JPanel jPanel = new JPanel();
    jPanel.removeAll();
    jPanel.setBorder(BorderFactory.createEmptyBorder(20,20,10,20));
    jPanel.setLayout(new GridLayoutManager(9, 2));
    SwingUtil.addChild(jPanel, new JLabel("Select project location: "), 0, 0);
    //重置选择输入框，避免弹出多次选择框
    textFieldWithBrowseButton = new TextFieldWithBrowseButton();
    FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(false, true, false, false, false, false);
    textFieldWithBrowseButton.addBrowseFolderListener(new TextBrowseFolderListener(fileChooserDescriptor));
    SwingUtil.addChild(jPanel, textFieldWithBrowseButton, 0, 1, true);
    SwingUtil.addChild(jPanel, new JLabel("Plugin ID: "), 1, 0);
    pluginID.setToolTipText("插件id");
    SwingUtil.addChild(jPanel, pluginID, 1, 1, true);
    SwingUtil.addChild(jPanel, new JLabel("Plugin Name: "), 2, 0);
    pluginName.setToolTipText("插件名称");
    SwingUtil.addChild(jPanel, pluginName, 2, 1, true);

    SwingUtil.addChild(jPanel, new JLabel("Plugin Category: "), 3, 0);
    pluginCategory.setToolTipText("插件编码");
    SwingUtil.addChild(jPanel, pluginCategory, 3, 1, true);


    SwingUtil.addChild(jPanel, new JLabel("Static directory: "), 4, 0);
    staticDirectory.setText(directory);
    staticDirectory.setToolTipText("编译项目目录");
    SwingUtil.addChild(jPanel, staticDirectory, 4, 1, true);
    SwingUtil.addChild(jPanel, new JLabel("Remote port: "), 5, 0);
    remotePort.setText(port);
    remotePort.setMargin(JBUI.emptyInsets());
    remotePort.setToolTipText("远程调试端口");
    SwingUtil.addChild(jPanel, remotePort, 5, 1, true);
    SwingUtil.addChild(jPanel, new JLabel(), 6);
    SwingUtil.addChild(jPanel, new JLabel(SwingUtil.promptText()), 7);
    SwingUtil.addChild(jPanel, new Spacer(), 8, 0, true);
    return jPanel;
  }

  @Override
  public void updateDataModel() {
  }

  @Override
  public boolean validate() throws ConfigurationException {
    if (textFieldWithBrowseButton.getText().equals("")
        || pluginID.getText().equals("")
        || pluginName.getText().equals("")
        || pluginCategory.getText().equals("")
        || staticDirectory.getText().equals("")
        || remotePort.getText().equals("")) {
      throw new ConfigurationException("Please fill in all Input box!");
    } else if (!SwingUtil.IsEnglish(pluginID.getText())) {
      throw new ConfigurationException("PluginID Must be English letters!");
    } else if (SwingUtil.IsItANumber(pluginCategory.getText())) {
      throw new ConfigurationException("Plugin Category only accepts Integer types!");
    } else if (SwingUtil.IsItANumber(remotePort.getText())) {
      throw new ConfigurationException("Remote port only accepts Integer types!");
    }
    return true;
  }

  public void clear() {
    textFieldWithBrowseButton.setText("");
    pluginID.setText("");
    pluginName.setText("");
    pluginCategory.setText("");
    staticDirectory.setText(directory);
    remotePort.setText(port);
  }
}
